new Vue({
	el: '#app',
	data: {
		uid: '',
		avatar: '',
		avatar_path: '',
		user: '',
		path: '',
		api_token: '',
		nickname: '昵称'
	},
	mounted: function() {
		this.init();
	},
	methods: {
		init: function() {
			var self = this;
			self.uid = $api.getStorage('uid');
			self.api_token = $api.getStorage('api_token');
			self.avatar = $api.getStorage('avatar');
			self.avatar_path = $api.getStorage('avatar_path');
			self.nickname = $api.getStorage('nickname');
			if(self.uid) {
				self.getUserMsg();
			} else {
				apiready = function() {
					self.layout();
				};
			}
		},
		close: function() {
			api.closeWin()
		},
		//登出
		layout: function() {
			api.openWin({
				name: 'login',
				url: '../../html/login/login.html',
				reload: true,
				vScrollBarEnabled: false,
				pageParam: {
					type: 2
				}
			});
		},
		getUserMsg: function() {
			var self = this;
			apiready = function() {
				var f = api.pageParam;
				self.fname = f.name;
				if(self.fname) {
					api.closeWin({
						name: self.fname
					});
				} else {

				}
				self.getMsg();
			};
		},
		//读取用户信息
		getMsg: function() {
			var self = this;
			console.log(self.uid);
			var data = {
				id: self.uid
			};
			$.post(base + config.user_show, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.user = msg.data;
					self.nickname = msg.data.nickname;
					if(self.user.integral == null) {
						self.user.integral = 0;
					} else {

					}
					self.path = msg.url;
				} else {
					console.log(self.error_msg);
					api.toast({
						msg: self.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		loyout: function() {
			var self = this;
			api.confirm({
				title: '是否退出',
				buttons: ['确定', '取消']
			}, function(ret, err) {
				var index = ret.buttonIndex;
				console.log(index);
				if(index == 1) {
					self.logout();
				}
			});
		},
		//退出登录
		logout: function() {
			var self = this;
			console.log(self.uid);
			console.log(self.api_token);
			var data = {
				"token": self.api_token,
				"uid": self.uid
			};
			$api.post(base + config.guest_logout, {
				values: data
			}, function(ret) {
				console.log(ret);
				var msg = $api.strToJson(ret);
				if(msg.error == 0) {
					$api.clearStorage();
					self.layout();
				} else {
					console.log(msg.msg);
					api.toast({
						msg: msg.msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'text');
		},
		//跳转修改个人信息
		editPersonMsg: function() {
			var self = this;
			if(self.uid) {
				api.openWin({
					name: "user-msg",
					url: "editUserMsg.html",
					vScrollBarEnabled: false,
				});
			} else {
				apiready = function() {
					self.layout();
				};
			}
		},
		//跳转修改头像
		editAvatar: function() {
			api.toast({
				msg: "暂无",
				duration: 2000,
				location: 'middle'
			});
		},
		//跳转修改密码
		editPassword: function() {
			var self = this;
			if(self.uid) {
				api.openWin({
					name: "editPassword",
					url: "editPassword.html",
					vScrollBarEnabled: false,
				});
			} else {
				apiready = function() {
					self.layout();
				};
			}
		},
		//跳转修改绑定手机号
		editPhone: function() {
			var self = this;
			if(self.uid) {
				api.openWin({
					name: "editPhone",
					url: "editPhone.html",
					vScrollBarEnabled: false,
					//					reload: true
				});
			} else {
				apiready = function() {
					self.layout();
				};
			}
		}
	}
})